const basicAuth = require('basic-auth')
const jwt = require('jsonwebtoken')
const {secretKey} = require('./tokentime').security
const result = require('@/config/handle')

class Authuid{
    get m() {
        return async (ctx, next) => {
            const token = basicAuth(ctx.req);
            let uid;   
            if (!token ||!token.name) {
                uid = null;
            } else {
                try {
                    let authcode = jwt.verify(token.name, secretKey);
                    uid = authcode;
                } catch (error) {
                    uid = null;
                }
            }
            ctx.auth = { uid }; 
            await next();
        };
    }
}

module.exports = {
    Authuid
}